﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.Compilation;

namespace Doo.ServerControls
{
    public class DropDownListEx : DropDownList
    {
        public string TypeName { get; set; }

        public string FilterString { get; set; }

        public int Filter { get; set; }

        private bool hasload = false;

        public void EnsureTypeLoaded()
        {
            if (hasload) return;
            Dictionary<string, string> values;
            Type type = BuildManager.GetType(TypeName, /*throwOnFail*/true, /*ignoreCase*/true);
            if (!string.IsNullOrEmpty(FilterString))
            {
                values = Doo.Extension.CodeMeaningHelper.GetCodeMeanings(type, FilterString);
            }
            else
            {
                values = Doo.Extension.CodeMeaningHelper.GetCodeMeanings(type, Filter);
            }
            foreach (var entry in values)
            {
                this.Items.Add(new ListItem(entry.Value, entry.Key));
            }
            this.EnableViewState = false;
            hasload = true;
        }

        protected override void OnDataBinding(EventArgs e)
        {
            EnsureTypeLoaded();
            base.OnDataBinding(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            EnsureTypeLoaded();
            base.OnLoad(e);
        }
    }
}
